package com.eastcom.collection.tools.common;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	public static void main(String[] args) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date date = new Date();
		Date nd = DateUtils.getLastMonthFirstDay(date);
		System.out.println(sdf.format(nd));

		nd = DateUtils.getLastMonthFinalDay(date);
		System.out.println(sdf.format(nd));

		nd = DateUtils.getThisMonthFirstDay(date);
		System.out.println(sdf.format(nd));

		nd = DateUtils.getThisMonthFinalDay(date);
		System.out.println(sdf.format(nd));

		nd = DateUtils.getLastDayFirstHour(date);
		System.out.println(sdf.format(nd));

		nd = DateUtils.getThisDayFirstHour(date);
		System.out.println(sdf.format(nd));
	}

	public static Date getLastMonthFirstDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, -1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		truncatHour(c);
		return c.getTime();
	}

	public static Date getLastMonthFinalDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, -1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.roll(Calendar.DAY_OF_MONTH, -1);
		truncatHour(c);
		return c.getTime();
	}

	public static Date getThisMonthFirstDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, 1);
		truncatHour(c);
		return c.getTime();
	}

	public static Date getThisMonthFinalDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.roll(Calendar.DAY_OF_MONTH, -1);
		truncatHour(c);
		return c.getTime();
	}

	public static Date getLastDayFirstHour(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, -1);
		truncatHour(c);
		return c.getTime();
	}

	public static Date getThisDayFirstHour(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		truncatHour(c);
		return c.getTime();
	}

	private static void truncatHour(Calendar c) {
		c.set(Calendar.HOUR_OF_DAY, 0);
		truncatMin(c);
	}

	private static void truncatMin(Calendar c) {
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
	}

	public static int getDayOfMonth(Calendar aCalendar){
//		Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
		int day=aCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		return day;
	}
	
	
}
